//
//  GJNetWorkErrorView.m
//  GanJie
//
//  Created by EDZ on 2018/5/14.
//  Copyright © 2018年 GanJie. All rights reserved.
//

#import "GJNetWorkErrorView.h"

@implementation GJNetWorkErrorView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    
    return self;
}

- (void)createView{
    
    
    UIView *backView = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:backView];
    _errorBackView = backView;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_message_no_data"]];
    [_errorBackView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(10);
//        make.size.equalTo(CGSizeMake(230*GJHeight, 115*GJHeight));
        make.size.equalTo(CGSizeMake(210*GJHeight, 150*GJHeight));
    }];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"哎呀！当前网络不太好！";
    label.font = [UIFont systemFontOfSize:16];
    [_errorBackView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(0*GJHeight);
        make.centerX.equalTo(imageView);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_errorBackView addSubview:button];
    [button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"重新加载" forState:0];
    [button  setTitleColor:[UIColor blackColor] forState:0];
    ViewBorderRadius(button, 5, 1, [UIColor blackColor]);
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView);
        make.top.equalTo(label.bottom).offset(30*GJHeight);
//        make.size.equalTo(CGSizeMake(170*GJHeight, 40*GJHeight));
        make.size.equalTo(CGSizeMake(80*GJHeight, 33*GJHeight));
    }];
    
    _imageView = imageView;
    _label = label;
    _button = button;
    
    imageView.image = [UIImage imageNamed:@"Nosignal"];
//    label.text = @"您的网络可能出现了问题，请稍后";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:15];
    button.backgroundColor = COLOR_MAIN;
    ViewBorderRadius(button, 3*GJHeight, 0, [UIColor blackColor]);
    [button setTitleColor:[UIColor whiteColor] forState:0];
    button.titleLabel.font = [UIFont systemFontOfSize:15*GJHeight];

    
    
    UIView *backView2 = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:backView2];
    backView2.hidden = YES;
    _noDataBackView = backView2;
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Nodataavailable"]];
    [_noDataBackView addSubview:imageView2];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self).offset(22*GJHeight);
        make.centerX.equalTo(self);
        make.top.equalTo(20*GJHeight);
//        make.size.equalTo(CGSizeMake(224*GJHeight, 160*GJHeight));
        make.size.equalTo(CGSizeMake(185*GJHeight, 150*GJHeight));
    }];
    imageView2.image = [UIImage imageNamed:@"Nodataavailable"];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.text = @"当前页面暂无数据！";
    label2.textColor = SubTitleColor;
    label2.font = [UIFont systemFontOfSize:14];
    [_noDataBackView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView2.bottom).offset(10*GJHeight);
        make.centerX.equalTo(self);
    }];
    _noDataimageView = imageView2;
    _noDatalabel = label2;
    
    
    
}
- (void)actionButton:(UIButton *)button{
    
    if (self.netWorkButtonBlock) {
        self.netWorkButtonBlock();
    }
    
}
- (void)updateType:(NSInteger)type{
    
    self.hidden = NO;
//    1默认断网 2无数据
    if (type == 2) {
        _noDataBackView.hidden = NO;
        _errorBackView.hidden = YES;
    }else {
        _noDataBackView.hidden = YES;
        _errorBackView.hidden = NO;
    }
    
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
